/*
 * TelaPreJogo.java
 *
 * Created on 28 de Março de 2009, 17:29
 */
package iu;

import comp.racer.common.rn.Jogador;
import comp.racer.common.rn.JogadorObserver;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import so.Cliente;

/**
 *
 * @author  Germano
 */
public class SalaDeEsperaTela extends TelaPadrao implements JogadorObserver {

    /** Creates new form TelaPreJogo */
    public SalaDeEsperaTela() {
        Jogador.attach(this);
        initComponents();
        updateComponents();
        exibeTela();

    }

    public void updateComponents() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        atualizaTabela();
        getLbIniciandoPartida().setVisible(false);
    }

    //metodo de refatoração
    private void atualizaTabela() {
        String[] colunas = new String[]{"Login", "Estado"};
        String[][] dados = new String[][]{};
        DefaultTableModel tableModel = new DefaultTableModel(dados, colunas);

        getTabelaJogadorStatus().setModel(tableModel);

        for (Jogador jogador : getLoginJogadorMap().values()) {

            if (jogador.getEstado() == Jogador.N_PREPARADO) {
                tableModel.addRow(new String[]{jogador.getLogin(), "Não preparado"});
            } else if (jogador.getEstado() == Jogador.PREPARADO) {
                tableModel.addRow(new String[]{jogador.getLogin(), "Preparado"});

            }
        }
    }

    
    public void update() {

        atualizaTabela();

    }

    private void notificaMudancaEstato() {

        getCliente().notificaMudancaEstado();

    }

    //gets e sets
    public JTable getTabelaJogadorStatus() {
        return tabelaJogadorStatus;
    }

    public void setTabelaJogadorStatus(JTable tabelaJogadorStatus) {
        this.tabelaJogadorStatus = tabelaJogadorStatus;
    }

 
    public Map<String, Jogador> getLoginJogadorMap() {
        return getCliente().getLoginJogadorMap();
    }

    public JLabel getLbIniciandoPartida() {
        return lbIniciandoPartida;
    }

    public void setLbIniciandoPartida(JLabel lbIniciandoPartida) {
        this.lbIniciandoPartida = lbIniciandoPartida;
    }

 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaJogadorStatus = new javax.swing.JTable();
        jogarBt = new javax.swing.JButton();
        rankingBt = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        sairBt = new javax.swing.JButton();
        lbIniciandoPartida = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CompRacer - Sala de Espera");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jScrollPane1.setViewportView(tabelaJogadorStatus);

        jogarBt.setText("Jogar");
        jogarBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jogarBtActionPerformed(evt);
            }
        });

        rankingBt.setText("Ranking");
        rankingBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rankingBtActionPerformed(evt);
            }
        });

        jLabel1.setText("CompRacer");

        sairBt.setText("Sair");
        sairBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sairBtActionPerformed(evt);
            }
        });

        lbIniciandoPartida.setText("jLabel2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(sairBt, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                                .addComponent(rankingBt, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jogarBt, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(182, 182, 182)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbIniciandoPartida, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jogarBt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rankingBt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sairBt))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addComponent(lbIniciandoPartida)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jogarBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jogarBtActionPerformed

        notificaMudancaEstato();

}//GEN-LAST:event_jogarBtActionPerformed

private void rankingBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rankingBtActionPerformed

    getCliente().iniciaTelaRanking();
}//GEN-LAST:event_rankingBtActionPerformed

private void sairBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sairBtActionPerformed
    
    finalizaPrograma();

}//GEN-LAST:event_sairBtActionPerformed



private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    finalizaPrograma();
}//GEN-LAST:event_formWindowClosing


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jogarBt;
    private javax.swing.JLabel lbIniciandoPartida;
    private javax.swing.JButton rankingBt;
    private javax.swing.JButton sairBt;
    private javax.swing.JTable tabelaJogadorStatus;
    // End of variables declaration//GEN-END:variables
}
